package test.hxy.mongoSchoolSimple.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import test.hxy.mongoSchoolSimple.DTO.Response;
import test.hxy.mongoSchoolSimple.model.Book;
import test.hxy.mongoSchoolSimple.model.Teacher;
import test.hxy.mongoSchoolSimple.service.BookService;
import test.hxy.mongoSchoolSimple.service.CountService;
import test.hxy.mongoSchoolSimple.service.TeacherService;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teacher")
public class TeacherController extends BaseController {
	@Autowired
	TeacherService teacherService;

	@Autowired
	CountService countService;

	@GetMapping("/findAll")
	@ResponseBody
	public Response<List<Teacher>> findAll(){
		List<Teacher> teacher = teacherService.findAll();
		if (teacher.size() == 0) {
			return getFailResult(404, "没找到!");
		} else {
			return getSuccessResult(teacher);
		}
	}

	@GetMapping("/findByTeacherNameContains")
	@ResponseBody
	public Response<List<Teacher>> findByTeacherNameContains(@RequestParam String teacherName){
		List<Teacher> teacher = teacherService.findByTeacherNameContains(teacherName);
		if (teacher.size() == 0) {
			return getFailResult(404, "没找到!");
		} else {
			return getSuccessResult(teacher);
		}
	}

	@GetMapping("/findByTeacherIdEquals")
	@ResponseBody
	public Response<Teacher> findByTeacherIdEquals(@RequestParam String teacherId){
		Teacher teacher = teacherService.findByTeacherIdEquals(teacherId);
		if (teacher == null) {
			return getFailResult(404, "没找到!");
		} else {
			return getSuccessResult(teacher);
		}
	}


	@GetMapping("/findByCampusIdEquals")
	@ResponseBody
	public Response<List<Teacher>> findByCampusIdEquals(@RequestParam String campusId){
		List<Teacher> teacher = teacherService.findByCampusIdEquals(campusId);
		if (teacher == null) {
			return getFailResult(404, "没找到!");
		} else {
			return getSuccessResult(teacher);
		}
	}

	@PostMapping("/insert")
	@ResponseBody
	public Response<Teacher> insertTeacher(@RequestBody Teacher teacher) {
		Teacher temp = teacherService.insertTeacher(teacher);
		if (temp == null) {
			return getFailResult(400, "ID已存在!");
		} else {
			return getSuccessResult(temp);
		}
	}

	@GetMapping("/delete")
	@ResponseBody
	public Response<Teacher> delete(@RequestParam String teacherId){
		Teacher teacher = teacherService.delete(teacherId);
		if (teacher == null) {
			return getFailResult(404, "没找到!");
		} else {
			return getSuccessResult(teacher);
		}
	}

	@PostMapping("/update")
	@ResponseBody
	public Response<String> update(@RequestBody Teacher teacher) {
		if (teacherService.delete(teacher.getTeacherId()) != null) {
			teacherService.insertTeacher(teacher);
			return getSuccessResult("Success");
		} else {
			return getFailResult(404, "不存在该数据");
		}
	}

	@GetMapping("/groupByCampusId")
	@ResponseBody
	public Response<List<Map>> groupByCampusId(){
		return getSuccessResult(countService.countTeachersGroupByCampus());
	}
}
